package com.xinzhidi.web.netty.controller;

import com.xinzhidi.common.annotation.Log;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.domain.R;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.common.enums.BusinessType;
import com.xinzhidi.common.utils.poi.ExcelUtil;
import com.xinzhidi.web.netty.entity.dto.DeviceInstructionAddDto;
import com.xinzhidi.web.netty.entity.dto.DeviceInstructionListDto;
import com.xinzhidi.web.netty.entity.dto.DeviceInstructionUpdDto;
import com.xinzhidi.web.netty.entity.vo.DeviceInstructionInfoVo;
import com.xinzhidi.web.netty.entity.vo.DeviceInstructionListVo;
import com.xinzhidi.web.netty.service.DeviceInstructionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 指令发送Controller
 *
 * @author cx
 * @date 2023-06-16
 */
@RestController
@Api(tags = "指令发送")
@RequestMapping("/deviceInstruction")
public class DeviceInstructionController extends BaseController {
    @Autowired
    private DeviceInstructionService deviceInstructionService;

    @ApiOperation("查询指令发送列表")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:list')")
    @PostMapping("/listDeviceInstruction")
    public TableDataInfo<DeviceInstructionListVo> listDeviceInstruction(@Validated @RequestBody DeviceInstructionListDto dto) {
        startPagePost(dto);
        return deviceInstructionService.selectDeviceInstructionList(dto);
    }

    @ApiOperation("导出指令发送列表")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:export')")
    @Log(title = "指令发送", businessType = BusinessType.EXPORT)
    @PostMapping("/exportDeviceInstruction")
    public void exportDeviceInstruction(HttpServletResponse response, DeviceInstructionListDto dto) {
        TableDataInfo<DeviceInstructionListVo> list = deviceInstructionService.selectDeviceInstructionList(dto);
        ExcelUtil<DeviceInstructionListVo> util = new ExcelUtil<DeviceInstructionListVo>(DeviceInstructionListVo.class);
        util.exportExcel(response, list.getRows(), "指令发送数据");
    }

    @ApiOperation("获取指令发送详细信息")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:query')")
    @GetMapping(value = "/getDeviceInstructionById")
    public R<DeviceInstructionInfoVo> getDeviceInstructionById(@RequestParam("id") Long id) {
        return R.ok(deviceInstructionService.selectDeviceInstructionById(id));
    }

    @ApiOperation("新增指令发送")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:add')")
    @Log(title = "指令发送", businessType = BusinessType.INSERT)
    @PostMapping("addDeviceInstruction")
    public AjaxResult addDeviceInstruction(@Validated @RequestBody DeviceInstructionAddDto dto) {
        return toAjax(deviceInstructionService.insertDeviceInstruction(dto));
    }

    @ApiOperation("修改指令发送")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:edit')")
    @Log(title = "指令发送", businessType = BusinessType.UPDATE)
    @PostMapping("editDeviceInstruction")
    public AjaxResult editDeviceInstruction(@Validated @RequestBody DeviceInstructionUpdDto dto) {
        return toAjax(deviceInstructionService.updateDeviceInstruction(dto));
    }

    @ApiOperation("删除指令发送")
    @PreAuthorize("@ss.hasPermi('deviceInstruction:deviceInstruction:remove')")
    @Log(title = "指令发送", businessType = BusinessType.DELETE)
    @DeleteMapping("/delDeviceInstruction")
    public AjaxResult delDeviceInstruction(@RequestParam Long[] ids) {
        return toAjax(deviceInstructionService.deleteDeviceInstructionByIds(ids));
    }
}
